﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Automation;
using Compiled_Tests.AutomationAbstractions;
using Tedds_Tool.Instructions;
using LEET.CommonInterfaces.Automation;
using LEET.LEETCommon.AutomationAbstractions;
using LEET.CommonInterfaces.Models;
using LEET.LEETCommon.Model;

namespace Tedds_Tool.Automatables
{
    public class Scrollable //: ILEETModual
    {
        public AutomationElementEventListener CreateListener(IObservableWidget elem, IRegistryAccessor reg)
        {
            return new EventListener(elem, reg);
        }

        public bool CanCompile(string instruction)
        {
            try
            {
                throw new NotImplementedException();
                //return "ACTION".Equals(parts[0]) && TypeName.Equals(parts[1]);

            }
            catch (InvalidInstructionException)
            {
                return false;
            }
        }

        public IInstruction CreateInstruction(string instruction)
        {
            throw new NotImplementedException();
        }

        

        private class EventListener : AutomationElementEventListenerBase
        {

            AutomationPropertyChangedEventHandler handler;
            private IRegistryAccessor registry;

            public EventListener(IObservableWidget target, IRegistryAccessor reg)
            {
                this.RootTarget = target;
                this.registry = reg;
                Listening = false;
                try
                {
                    handler = new AutomationPropertyChangedEventHandler(CatchEvent);
                    registry.RegisterEventHandler(RangeValuePatternIdentifiers.ValueProperty, RootTarget.ae, handler);
                    Listening = true;
                }
                catch (ArgumentException) { }
            }

            public override void Listen(IObservableWidget widget)
            {
                //In case element disappeared before it could be processed
                if (widget == null) return;

                string element = widget.GetUIAHierarchy(RootTarget).ToString();
                Delagate(new Event_Pair("ACTION\t| SCROLL | " + element));
            }

            public override void Dispose()
            {
                registry.RemoveEventHandler(RangeValuePatternIdentifiers.ValueProperty, RootTarget.ae, handler);
                base.Dispose();
            }

        }
    }
}
